<template>
  <div class="login-container">
    <div id="login_header" class="login_header">
      <div class="title">舌尖炸串铺</div>
    </div>
    <!-- 中间 -->
    <div class="login_center" @mouseover="animationOn" @mouseout="animationOff">
      <!-- 表单 -->
      <div class="form">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          autocomplete="on"
          label-position="left"
        >
          <div style="text-align:center;height:60px;line-height:60px;font-weight:bold;color:#e693b8;font-size: 30px;">登录
          </div>
          <el-form-item prop="username">
            <el-input ref="username" v-model="loginForm.username" placeholder="Username" type="text" />
          </el-form-item>
          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
            <el-form-item prop="password">
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                placeholder="Password"
              />
            </el-form-item>
          </el-tooltip>
          <div>
            <el-button :loading="loading" style="width:100%;" @click.native.prevent="handleLogin">登录</el-button>
          </div>
        </el-form>
        <!-- 表单 -->
      </div>
    </div>
    <div class="footer">研发小组</div>
  </div>
</template>

<script>
import { post } from '@/utils/request'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      callback()
    }
    const validatePassword = (rule, value, callback) => {
      callback()
    }
    return {
      visible: false,
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    // 提交注册
    submitHandler() {
      this.$refs['registerForm'].validate((valid) => {
        if (valid) {
          const url = '/user/register'
          post(url, this.form).then((response) => {
            this.$message({ type: 'success', message: response.message })
            this.visible = false
          })
        } else {
          return false
        }
      })
    },
    // 重置表单校验
    clearValidate() {
      this.$refs['registerForm'].clearValidate()
    },
    // 去保存
    toRegisterHandler() {
      this.form = {}
      this.visible = true
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    animationOn() {
      const anOff = document.getElementById('login_header')
      anOff.style.animationPlayState = 'paused'
    },
    animationOff() {
      const anOff = document.getElementById('login_header')
      anOff.style.animationPlayState = 'running'
    }
  }

}
</script>

<style lang="scss">
/* reset element-ui css */
.login-container {
  position: absolute;
  width: 100%;
  top: 0;
  bottom: 0;
  background-image: linear-gradient(to right, #b2a3ce, #d76d77, #ffaf7b);
  overflow: hidden;

  .login_header {
    height: 300px;
    position: relative;
    color: #ffffff;
    background-color: antiquewhite;
    border-bottom-left-radius: 30vw;
    border-bottom-right-radius: 30vw;
    width: 55vw;
    margin: auto;
    animation: move 2.5s linear infinite alternate;

    @keyframes move {
      0% {
        z-index: 0;
        height: 350px;
      }

      40% {
        z-index: 5;
        height: 300px;
      }

      80% {
        z-index: 0;
        height: 350px;
      }

      100% {
        z-index: 5;
        height: 300px;
      }
    }

    .title {
      text-align: center;
      position: absolute;
      bottom: 150px;
      width: 100%;
      font-size: 3em;
    }
  }

  .login_center {
    box-sizing: border-box;
    position: absolute;
    top: 30vh;
    left: 35vw;
    background-color: #ffffff;
    width: 500px;
    // margin: 0 auto;
    border-radius: 15px;
    padding: 4em 3em;
    box-shadow: 0 0 10px #ccc;
  }

  .login_header:hover+.login_center {
    z-index: 10;
  }

  .login_center:hover {
    z-index: 10;
  }

  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    line-height: 3em;
    text-align: center;
    font-size: 12px;
    font-weight: bold;
    color: wheat;
  }
}
</style>
